const BASE_URL = 'https://sfyk.yzsq2006.com/index.php'
uni.setStorageSync('baseUrl', BASE_URL)
import Vue from 'vue'
import store from '../store'
import {
	debounce
} from './util.js'

 function getwxCode() {
	uni.login({
		success(res) {
			let code = res.code
			console.log(code);
			showToast(code)
		}
	})
};

function showToast(code) {
	uni.showModal({
		title: '温馨提示',
		content: '亲，授权微信登录后才能正常使用小程序功能',
		success(res) {
			//如果用户点击了确定按钮
			if (res.confirm) {
				uni.getUserProfile({
					desc: '获取你的昵称、头像、地区及性别',
					success: res => {
						console.log(res);
						getUserInfo(code, res.userInfo)
					},
					fail: res => {
						//拒绝授权
						uni.showToast({
							title: '您拒绝了请求,不能正常使用小程序',
							icon: 'none',
							duration: 3000
						});
						return;
					}
				});
				
			} else if (res.cancel) {
				//如果用户点击了取消按钮
				uni.showToast({
					title: '您拒绝了请求,不能正常使用小程序',
					icon: 'none',
					duration: 3000
				});
				return;
			}
		}
	});
}

function getUserInfo(_code, _userInfo) {
	let that = this
	let form = {}
	form.code = _code
	form.userInfo = _userInfo
	let tjr_id = uni.getStorageSync('tjr_id')
	if (tjr_id > 0) {
		form.tjr_id = tjr_id
	}
	uni.request({
		url: `${BASE_URL}?s=/api/Passport/mpWxLogin`,
		method: 'POST',
		data: {
			form
		},
	}).then(data => {
		let [err, res] = data;
		if (res.data.status == 200) {
			uni.setStorageSync('Access-Token', res.data.data.token);
		}
	}).then(() => {
		uni.request({
			url: `${BASE_URL}?s=/api/User/info`,
			method: 'POST',
			header: {
				'Access-Token': uni.getStorageSync('Access-Token')
			},
		}).then(data => {
			let [err, res] = data;
			if (res.data.status == 200) {
				uni.setStorageSync('userInfo', res.data.data.userInfo);
				store.state.user.user_id = res.data.data.userInfo.user_id
				store.state.user.token = uni.getStorageSync('Access-Token')
				
				var pages = getCurrentPages(); // 当前页面
				var beforePage = pages[pages.length -
					1]; //
			
				uni.redirectTo({
					url: '/' + beforePage.route,
					complete(mes) {
						console.log(mes)
					}
				})
				// uni.reLaunch({
				//     url: '/pages/index/index',
				//     success(res) {
				//         console.log(res);
				//     },
				//     fail(err) {
				//         console.log(err);
				//     }
				// });
			}
		})
	})
};

export default (params) => {
	// 加载中
	uni.showLoading({
		title: '加载中'
	})

	let _this = Vue;

	return new Promise((resolve, reject) => {
		uni.request({
			...params,
			url: BASE_URL + params.url,
			method: params.method || 'POST',
			data: params.data || {},
			dataType: "json",
			header: {
				'Access-Token': uni.getStorageSync('Access-Token'),
				// 'content-type' : "application/x-www-form-urlencoded",
				// 'content-type' : "multipart/form-data",
			},
			success(res) {
				if (res.data.status == 401) {
					getwxCode()
				} else if (res.data.code != 1 && res.data.status != 200) {
					uni.showToast({
						title: '获取数据失败',
						duration: 3000,
						icon: 'error'
					});
				}
				resolve(res)
			},
			fail(err) {
				uni.showToast({
					title: '服务器错误',
					duration: 3000,
					icon: 'none'
				});
				reject(err)
			},
			complete() {
				// 隐藏加载中
				uni.hideLoading()
			}
		})
	})
}
